package com.ruoyi.web.controller.finance;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.web.common.ProjectConstant;
import com.ruoyi.web.entity.AssetPrice;
import com.ruoyi.web.entity.FundAsset;
import com.ruoyi.web.mapper.AssetPriceMapper;
import com.ruoyi.web.service.AccountInfoService;
import com.ruoyi.web.service.AssetService;
import com.ruoyi.web.service.FundAssetService;
import com.ruoyi.web.service.UserBalanceService;
import com.ruoyi.web.util.PageUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.List;

/**
 * 动账信息
 */
@Controller
@RequestMapping("/finance/asset")
public class AssetController {

    @Resource
    private FundAssetService fundAssetService;
    @Resource
    private AssetService assetService;
    @Resource
    private AssetPriceMapper assetPriceMapper;


    @GetMapping("assetList")
    public String assetList(Model modelAndView) {
        return "finance/asset/assetList";
    }

    @GetMapping("add")
    public String add(Model modelAndView) {
        return "finance/asset/add";
    }

    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        FundAsset one = fundAssetService.lambdaQuery()
                .eq(FundAsset::getId, id)
                .one();

        mmap.put("fundAsset", one);
        return "finance/asset/edit";
    }

    @GetMapping("/updatePrice/{id}")
    public String updatePrice(@PathVariable("id") Long id, ModelMap mmap) {
        FundAsset one = fundAssetService.lambdaQuery()
                .eq(FundAsset::getId, id)
                .one();

        AssetPrice assetPrice = assetService.fetchPrice(one);
        mmap.put("fundAsset", one);
        mmap.put("assetPrice", assetPrice);
        return "finance/asset/updatePrice";
    }


    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list() {
        /*动账明细*/
        LambdaQueryWrapper<FundAsset> queryWrapper = new QueryWrapper<FundAsset>()
                .lambda()
                .eq(FundAsset::getEndDate, ProjectConstant.FOREVER_DATE)
                .orderByDesc(FundAsset::getStartDate);

        Page<FundAsset> page = PageUtils.getPage();
        page = assetService.page(page, queryWrapper);

        return PageUtils.page2Table(page);
    }


    /**
     * 新增
     */
    @PostMapping("/add")
    @ResponseBody
    @Transactional
    public AjaxResult addSave(FundAsset fundAsset) {
        String assetName = fundAsset.getAssetName();
        List<FundAsset> list = fundAssetService.lambdaQuery()
                .eq(FundAsset::getEndDate, ProjectConstant.FOREVER_DATE)
                .eq(FundAsset::getAssetName, assetName)
                .list();
        if (CollectionUtils.isNotEmpty(list)) {
            return AjaxResult.error("该名称已存在");
        }
        LocalDate today = LocalDate.now();
        fundAsset.setStartDate(today);
        fundAsset.setEndDate(ProjectConstant.FOREVER_DATE);


        AssetPrice assetPrice = assetService.fetchPrice(fundAsset);
        if (assetPrice == null) {
            assetPrice = new AssetPrice();
            assetPrice.setTypeName(fundAsset.getTypeName());
            assetPrice.setAssetName(fundAsset.getAssetName());
            assetPrice.setPrice(fundAsset.getCostPrice());
            assetPrice.setStartDate(fundAsset.getStartDate());
            assetPrice.setEndDate(fundAsset.getEndDate());
            assetPriceMapper.insert(assetPrice);
        } else {
            assetService.updateAssetPrice(today, fundAsset.getTypeName(), fundAsset.getAssetName(), fundAsset.getCostOf());
        }

        fundAssetService.save(fundAsset);

        return AjaxResult.success();
    }

    /**
     * 修改
     */
    @PostMapping("/edit")
    @ResponseBody
    @Transactional
    public AjaxResult edit(FundAsset fundAsset) {
        fundAssetService.updateById(fundAsset);
        return AjaxResult.success();
    }

//    /**
//     * 修改
//     */
//    @PostMapping("/updatePrice")
//    @ResponseBody
//    @Transactional
//    public AjaxResult updatePrice(FundAsset fundAsset) {
//        LocalDate today = LocalDate.now();
//        FundAsset oldFundAsset = fundAssetService.getById(fundAsset.getId());
//
//        AssetPrice assetPrice = assetService.fetchPrice(oldFundAsset);
//        if (assetPrice == null) {
//            assetPrice = new AssetPrice();
//            assetPrice.setTypeName(fundAsset.getTypeName());
//            assetPrice.setAssetName(fundAsset.getAssetName());
//            // 借用字段，当市场单价
//            assetPrice.setPrice(fundAsset.getCostPrice());
//            assetPrice.setStartDate(today);
//            assetPrice.setEndDate(ProjectConstant.FOREVER_DATE);
//            assetPriceMapper.insert(assetPrice);
//        } else {
//            assetService.updateAssetPrice(today, fundAsset.getTypeName(), fundAsset.getAssetName(), fundAsset.getCostPrice());
//        }
//        return AjaxResult.success();
//    }


}
